import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {IknowledgeDetail} from '../utils/interface'
import {getDetail} from '../services/index'

export interface KnowledgeDetailModelState {
  list:IknowledgeDetail
}

export interface IndexModelType {
  namespace: 'knowledgeDetail';
  state: KnowledgeDetailModelState;
  effects: {
    getDetail: Effect;
  };
  reducers: {
    save: Reducer<KnowledgeDetailModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'knowledgeDetail',

  state: {
    list:{}
  },

  effects: {
    *getDetail({ payload }, { call, put }) {
      let res = yield call(getDetail,payload)
      console.log(res)
      yield put({
        type:'save',
        payload:{
          list:res.data
        }
      })
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'query',
          });
        }
      });
    },
  },
};

export default IndexModel;